﻿namespace FFF.Test.Application.AppServices.PublicService.VerificationCode
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Auditing;
    using Abp.Domain.Uow;
    using Abp.EntityFrameworkCore;
    using Abp.Runtime.Caching;
    using Abp.UI;
    using Abp.Web.Models;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using FFF.Test.Application.AppServices;
    using FFF.Test.Application.AppServices.PublicService.Sessions;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Entity;
    using FFF.Test.Util.Consts;
    using FFF.Test.Util.Web;
    using Ydm.Extensions;
    using Ydm.Helper;

    /// <summary>
    /// SsmsCode的管理对象
    /// </summary>
    [YdmApiVersionAttribute(Version = "My")]
    public class SsmsCodeAppService : YdmAbpAppServiceBase, ISsmsCodeAppService
    {
        private readonly ISessionAppService _sessionAppService;
        private readonly IHttpContextAccessor _httpContext;
        private readonly IDbContextProvider<MyDbContext> _dbContextProvider;
        private readonly IVerificationCodeAppService _verificationCodeAppService;

        /// <summary>
        /// Initializes a new instance of the <see cref="SsmsCodeAppService"/> class.
        /// SsmsCode构造方法
        /// </summary>
        /// <param name="sessionAppService">sesson服务</param>
        /// <param name="httpContext">context</param>
        /// <param name="dbContextProvider">dbcontext</param>
        /// <param name="verificationCodeAppService">图形验证码</param>
        public SsmsCodeAppService(
           ISessionAppService sessionAppService,
            IHttpContextAccessor httpContext,
             IDbContextProvider<MyDbContext> dbContextProvider,
              IVerificationCodeAppService verificationCodeAppService
            )
        {
            this._sessionAppService = sessionAppService;
            this._httpContext = httpContext;
            this._verificationCodeAppService = verificationCodeAppService;
        }

        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="inDto">发送短信输入dto</param>
        /// <returns>空</returns>
        [HttpPost]
        [AllowAnonymous]
        [UnitOfWork(isTransactional: false)]
        public async Task GenSsmsCode(SsmsInDto inDto)
        {
            string code = await this._sessionAppService.GetValidateCode();
            if (inDto.ValidateCode != code)
            {
                throw new UserFriendlyException("图形验证码错误");
            }

            if (inDto.Phone.Length != 11)
            {
                throw new UserFriendlyException("手机号错误");
            }

            // 刷新下验证码 防止重复调用
            await this._verificationCodeAppService.GenVerificationCode();

            // 发送短信
            string ssmCode = YdmVerifyCodeHelper.GetSingleObj().CreateVerifyCode(YdmVerifyCodeHelper.VerifyCodeType.NumberVerifyCode);

            ssmCode = "123456";

            // YdmSSMSHelper.SendAcs("keyid", "secret", "signanme", inDto.Phone, "SMS_166372385", new { code = ssmCode }, "ydmcore");
            this._httpContext.HttpContext.Session.SetString(AppConfigConsts.SsmsCodeStr, ssmCode);
        }
    }
}